// 生成容器
function createDom (template: string): any {
  if (template) {
    let wrapper = document.createElement('div')
    wrapper.innerHTML = template
    return wrapper.childNodes
  } else {
    console.error(`缺少template参数。`)
  }
}
// 主程序
function download (url: string, name = '下载') {

  if (!url) {
    return;
  }
  if (url != 'string') {
    let dataUrl = window.URL.createObjectURL(new Blob([ url ]));
    let link = document.createElement("a");
    link.style.display = "none";
    link.href = dataUrl;
    link.setAttribute("download", name + ".xls");
    document.body.appendChild(link);
    link.click();
    return
  }

  let container: any = document.querySelector('#dIframe')
  if (!container) {
    container = createDom(`<iframe src="" frameborder="0" id="dIframe"></iframe>`)[ 0 ]
    let body: any = document.querySelector('body')
    body.appendChild(container)
  }
  return new Promise((reso, rej) => {
    container.onerror = function (e: Event) {
      console.error(e)
      rej(e)
    }
    container.src = url
    reso(true)
  })
}

export {
  createDom,
  download
}
export default download
